#!/bin/bash
#EXEC=ex4.exe
test_dirs=( dan_assaf gershuni random )
manual_test_dirs=( dan_assaf gershuni )
test_num=$1
num_lines=$2
# compile
function make_exe {
	cd ..
	make
	if [ $? -ne 0 ]; then
	  echo "Compile of hw4.lex or hw4.ypp failed!"
	  exit 1
	fi
	cd ./tests/
}
# Run the tests

function to_unix {
	echo "dos2unix"
	for man_test_dir in $manual_test_dirs;
	do
		dos2unix `ls ${man_test_dir}/*.*`
	done
	dos2unix `ls ./gershuni/*.*`
}

function generate {	
	echo "num lines is $num_lines"
	echo "Generating tests"
	for i in $(seq 1 $test_num);
	do
		python weather.py $num_lines > random/random_$i.in 
		cat random/random_$i.in | ./hw4.exe > random/random_$i.out
	done
}

function run {
	echo "Running tests on ${test_dirs[@]}"
	for test_dir in ${test_dirs[*]} ;
	do
	  cd $test_dir
	  for test_file in `ls *.in` ;
	  do
		test="${test_file:0:${#test_file}-3}"
		echo -n "Running test ${test} ... "
		../../ex4.exe < $test.in > $test.res
		diff $test.res $test.out > $test.diff
		if [ -s $test.diff ]; then
		  echo "FAILED (see $test.diff)"
		else
		  echo "OK"
		fi
	  done
	  cd ..
	done
}

make_exe
to_unix
generate
run
